package com.mihu.stmall.manager.controller.api;


import com.mihu.stmall.manager.entiy.Admin;
import com.mihu.stmall.manager.entiy.Result;
import com.mihu.stmall.manager.service.AdminService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/admin")
@AllArgsConstructor
public class APIAdminController {

    private AdminService adminService;


    @PostMapping("/login")
    public Result login(Admin admin, String vercode, HttpSession session){
        //验证验证码是否正确
String sVercode = (String) session.getAttribute("vercode");


if (sVercode ==null ||
        !sVercode.equals(vercode.toUpperCase())){
    return Result.error("验证码错误");
        }
        try {
            admin = adminService.login(admin);
            //登录成功时
            admin.setPassword(null);
            admin.setSalt(null);
            session.setAttribute("admin",admin);

            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }
@GetMapping("/logout")
    public Result logout(HttpSession session){
        //清除session
    session.invalidate();

    return Result.success();

}


@PostMapping("/modifyPwd")
    public Result modifyPwd(String oldPassword,String newPassword,
                            String rePassword,HttpSession session){

        Admin admin = (Admin) session.getAttribute("admin");

    try {
        adminService.modifyPwd(admin.getId(),oldPassword,newPassword,rePassword);
        //成功
        return Result.success();

    } catch (Exception e) {
        e.printStackTrace();
        //失败
        return Result.error(e.getMessage());
    }
}
        @PutMapping
    public Result edit(Admin admin,HttpSession session){

        Admin loginAdmin = (Admin) session.getAttribute("admin");

        admin.setId(loginAdmin.getId());

            try {
                adminService.edit(admin);
                return Result.success();
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(e.getMessage());
            }

        }

        @GetMapping("/getLogin")

    public Result getLogin(HttpSession session){
        //获取当前登录的用户
        Admin admin = (Admin) session.getAttribute("admin");

        if (admin != null){
            return Result.success(admin);
        }else {
            return Result.error("未登录");
        }
        }


        @PostMapping("/add")
        public Result add(@RequestBody Admin admin){

            try {
                adminService.add(admin);
                return Result.success();
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(e.getMessage());
            }
        }

        @DeleteMapping("/remove")
    public Result remove(Admin admin,HttpSession session){
            Admin removeAdmin = (Admin) session.getAttribute("admin");

            admin.setId(removeAdmin.getId());
            try {
                adminService.remove(admin.getId());
                return Result.success();
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(e.getMessage());
            }

        }
}

